package com.dson.controller;

import com.dson.dao.ResumeDao;
import com.dson.pojo.Resume;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/resume")
public class ResumeController {

    @Autowired
    private ResumeDao resumeDao;

    @RequestMapping("/queryAll")
    public ModelAndView queyrAll(){
        List<Resume> resumes = resumeDao.queryAll();
        for (Resume resume: resumes){
            System.out.println(resume);
        }

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("resumeList",resumes);
        modelAndView.setViewName("table");

        return modelAndView;
    }

    @RequestMapping("/login")
    public String login(){
       return "redirect:/resume/queryAll";
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public String add(@RequestBody Resume resume){
        try{
            resumeDao.save(resume);
            return "success";
        }catch (Exception e){
            return "error："+e.getMessage();
        }
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public String update(@RequestBody Resume resume){
        try{
            resumeDao.save(resume);
            return "success";
        }catch (Exception e){
            return "error："+e.getMessage();
        }
    }

    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public String delete(@RequestBody  Long id) {
        try {
            resumeDao.deleteById(id);
            return "success";
        } catch (Exception e) {
            return "error：" + e.getMessage();
        }
    }

}
